package com.ict.web.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.api.client.util.Lists;
import com.ict.common.utils.DateUtils;
import com.ict.common.utils.StringUtils;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.system.dto.FileUploadResp;
import com.ict.system.service.FileService;
import com.ict.web.constant.Constants;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.controller.file.service.IYzqFileService;
import com.ict.web.domain.ProfessionalQualification;
import com.ict.web.domain.TechnicalTitle;
import com.ict.web.dto.InsertProfessionalQualificationDto;
import com.ict.web.dto.InsertTechnicalTitleDto;
import com.ict.web.dto.UpdateTechnicalTitleDto;
import com.ict.web.mapper.ExpertInformationSheetMapper;
import com.ict.web.mapper.TechnicalTitleMapper;
import com.ict.web.service.ITechnicalTitleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * 技术职称Service业务层处理
 * 
 * @author liujie
 * @date 2022-02-23
 */
@Service
public class TechnicalTitleServiceImpl extends ServiceImpl<TechnicalTitleMapper,TechnicalTitle> implements ITechnicalTitleService
{
    @Resource
    private TechnicalTitleMapper technicalTitleMapper;

    @Resource
    private IYzqFileService fileService;


    /**
     * 查询技术职称
     * 
     * @param technicalId 技术职称主键
     * @return 技术职称
     */
    @Override
    public TechnicalTitle selectTechnicalTitleByTechnicalId(Long technicalId)
    {
        return technicalTitleMapper.selectTechnicalTitleByTechnicalId(technicalId);
    }

    /**
     * 查询技术职称列表
     * 
     * @param
     * @return 技术职称
     */
    @Override
    public List<TechnicalTitle> selectTechnicalTitleList()
    {
        return technicalTitleMapper.selectTechnicalTitleList();
    }

    /**
     * 新增技术职称
     * 
     * @param addDtoS 技术职称
     * @return 结果
     */
    @Override
    public void insertTechnicalTitle(Long expertId,List<InsertTechnicalTitleDto> addDtoS)
    {
        if (!CollectionUtils.isEmpty(addDtoS)){
            List<TechnicalTitle> beans = Lists.newArrayList();
            for (InsertTechnicalTitleDto technicals :addDtoS){
                TechnicalTitle bean = InsertTechnicalTitleDto.of(technicals);
                //同步文件对象
                ArrayList<YzqFile> fileArrayList = InsertTechnicalTitleDto.toFile(bean, technicals);
                if(StringUtils.isNotEmpty(fileArrayList)){
                    fileService.batchInsertFile(fileArrayList);
                }

                bean.setExpertId(expertId);
                beans.add(bean);
//                beans.add(new TechnicalTitle(IdUtils.snowId(),expertId,technicals.getJobTitle(), technicals.getPassingTime(),
//                        technicals.getDeadline(),technicals.getTechnicalFile(),TechnicalTitle.DELETE_STATUS_NORMAL,
//                       LocalDateTime.now(),LocalDateTime.now()));
            }
            this.saveBatch(beans);
        }
    }

    /**
     * 修改技术职称
     * 
     * @param dto 技术职称
     * @return 结果
     */
    @Override
    public int updateTechnicalTitle(UpdateTechnicalTitleDto dto)
    {
        TechnicalTitle bean = technicalTitleMapper.selectById(dto.getTechnicalId());
        Assert.isTrue(bean != null && !TechnicalTitle.DELETE_STATUS_UNNOTAL.equals(bean.getDelFlag()),"数据不存在!!!");
        BeanUtils.copyProperties(dto, bean);
        bean.setUpdateTime(LocalDateTime.now());
        return technicalTitleMapper.updateById(bean);
    }


    /**
     * 删除技术职称信息
     * 
     * @param technicalId 技术职称主键
     * @return 结果
     */
    @Override
    public int deleteTechnicalTitleByTechnicalId(Long technicalId)
    {
        TechnicalTitle bean = technicalTitleMapper.selectById(technicalId);
        bean.setDelFlag("1");
        return technicalTitleMapper.updateById(bean);
    }
}
